package com.hnust.root.service.auth;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hnust.root.common.model.ManagerInfo;
import com.hnust.root.common.model.SysRole;
import com.hnust.root.dao.entity.domain.*;
import com.hnust.root.dao.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 后台用户管理
 */

@Service
public class ManagerInfoService {

    private static final Log LOGGER = LogFactory.get();

    @Autowired
    private ManagerMapper managerMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private ManagerToRoleMapper managerToRoleMapper;
    /**
     * 通过名称查找用户
     *
     * @param username
     * @return
     */
    public ManagerInfo findByUsername(String username) {
        Manager manager = managerMapper.selectOne(new QueryWrapper<Manager>()
                .eq("username", username));
        if (manager != null) {
            ManagerInfo managerInfo = new ManagerInfo();
            assmbleManagerInfo(managerInfo, manager);

            List<ManagerToRole> managerToRoleList = managerToRoleMapper.selectList(new QueryWrapper<ManagerToRole>()
                    .eq("sys_user_id", manager.getId()));

            List<Role> roleList = new ArrayList<>();
            managerToRoleList.forEach(managerToRole -> {
                Role role = roleMapper.selectById(managerToRole.getRoleId());
                if (role != null) {
                    roleList.add(role);
                }
            });

            List<SysRole> sysRoleList = new ArrayList<>();
            //先只看角色，不看权限
            roleList.forEach(role -> {
                sysRoleList.add(assmbleSysRole(role));
            });
            managerInfo.setRoles(sysRoleList);
            return managerInfo;
        } else {
            return null;
        }
    }

    /***
     * 组装managerInfo
     * @param manager
     */
    private void assmbleManagerInfo(ManagerInfo managerInfo, Manager manager) {
        managerInfo.setId(manager.getId());
        managerInfo.setName(manager.getName());
        managerInfo.setUsername(manager.getUsername());
        managerInfo.setPassword(manager.getPassword());
        managerInfo.setPhone(manager.getPhone());
        managerInfo.setSalt(manager.getSalt());
        managerInfo.setCreateTime(manager.getCreateTime());
        managerInfo.setState(manager.getState());
        managerInfo.setTips(manager.getTips());
        managerInfo.setUpdateTime(manager.getUpdateTime());
    }

    private SysRole assmbleSysRole(Role role) {
        SysRole sysRole = new SysRole();
        sysRole.setId(role.getId());
        sysRole.setDescription(role.getDescription());
        sysRole.setRole(role.getRole());
        sysRole.setCreateTime(role.getCreateTime());
        sysRole.setUpdateTime(role.getUpdateTime());
        return sysRole;
    }

    @Async
    public void testAysn() {
        try {
            Thread.sleep(5000);
            LOGGER.info("异步任务执行完成");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
